package com.louis.iread.bean;

import com.louis.iread.skin.SkinMgr;
import com.louis.iread.utils.SharedPreferencesUtil;

/**
 * Created by louis on 17-5-19.
 */

public class Config {
    //夜间模式设置
    public static final String THEME = "theme";
    public static final String NIGHT = "night";
    public static final String DAY = "";
    public static final String PRE_LOAD = "preLoad";
    public static final String TEXT_SIZE = "textSize";
    public static final String UPDATE_ON_START = "update_on_start";
    public static final String SORT_TYPE = "sort_type";
    public static final String LINE_SPACE = "line_space";

    public static final int    DEFAULT_TEXT_SIZE = 18;
    public static final int    MIN_TEXT_SIZE = 5;
    public static final int    MAX_TEXT_SIZE = 100;
    public static final int    DEFAULT_PRE_LOAD = 10;

    public static final String READ_BG = "read_background";
    public static String readBg = null;

    public static String getReadBg() {
        if (readBg != null) {
            return readBg;
        }
        return SharedPreferencesUtil.getInstance().getString(Config.READ_BG, "gray");
    }

    public static void setReadBg(String bg) {
        if (null == bg) {
            return;
        }
        SharedPreferencesUtil.getInstance().putString(Config.READ_BG, bg);
    }

    private static String themeName = null;
    public static String getTheme() {
        if (themeName != null) {
            return themeName;
        }
        return SharedPreferencesUtil.getInstance().getString(Config.THEME, "");
    }
    public static void setTheme(String themeName) {
        if (null == themeName) {
            return;
        }
        String currentTheme = getTheme();
        if (themeName.equals(currentTheme)) {
            return;
        }
        Config.themeName = themeName;
        SharedPreferencesUtil.getInstance().putString(Config.THEME, themeName);
    }

    public static boolean getUpdateOnStart() {
        return SharedPreferencesUtil.getInstance().getBoolean(Config.UPDATE_ON_START, true);
    }

    public static void setUpdateOnStart(boolean yes) {
        SharedPreferencesUtil.getInstance().putBoolean(Config.UPDATE_ON_START, yes);
    }

    public static int getTextSize() {
        return SharedPreferencesUtil.getInstance().getInt(Config.TEXT_SIZE, Config.DEFAULT_TEXT_SIZE);
    }

    public static void setTextSize(int textSize) {
        int size = Config.DEFAULT_TEXT_SIZE;
        if (textSize < MIN_TEXT_SIZE) {
            size = MIN_TEXT_SIZE;
        } else if (textSize > MAX_TEXT_SIZE) {
            size = MAX_TEXT_SIZE;
        } else {
            size = textSize;
        }
        SharedPreferencesUtil.getInstance().putInt(Config.TEXT_SIZE, size);
    }

    public static void setPreLoad(int num) {
        if (num <= 0) {
            SharedPreferencesUtil.getInstance().putInt(PRE_LOAD, DEFAULT_PRE_LOAD);
        } else {
            SharedPreferencesUtil.getInstance().putInt(PRE_LOAD, num);
        }
    }

    public static int getPreLoad() {
        return SharedPreferencesUtil.getInstance().getInt(Config.PRE_LOAD, Config.DEFAULT_PRE_LOAD);
    }

    private static String sortType = null;
    public static String SORT_TYPE_READ = "readtime";
    public static String SORT_TYPE_UPDATE = "updatetime";
    public static void setSortType(String sortType) {
        if (SORT_TYPE_READ.equals(sortType) || SORT_TYPE_UPDATE.equals(sortType)) {
            SharedPreferencesUtil.getInstance().putString(SORT_TYPE, sortType);
            Config.sortType = sortType;
        } else {
            SharedPreferencesUtil.getInstance().putString(SORT_TYPE, SORT_TYPE_READ);
            Config.sortType = SORT_TYPE_READ;
        }
    }

    public static String getSortType() {
        if (Config.sortType != null) {
            return Config.sortType;
        }
        return SharedPreferencesUtil.getInstance().getString(Config.SORT_TYPE, Config.SORT_TYPE_READ);
    }

    private static String pageMethod = null;
    public static String PAGE_METHOD = "page_method";
    public static String PAGE_NONE = "none";
    public static String PAGE_SLIDE = "slide";
    public static void setPageMethod(String method) {
        if (PAGE_NONE.equals(method) || PAGE_SLIDE.equals(method)) {
            SharedPreferencesUtil.getInstance().putString(PAGE_METHOD, method);
            Config.pageMethod = method;
        } else {
            SharedPreferencesUtil.getInstance().putString(PAGE_METHOD, PAGE_SLIDE);
            Config.pageMethod = PAGE_SLIDE;
        }
    }

    public static String getPageMethod() {
        if (pageMethod != null) {
            return Config.pageMethod;
        }
        return SharedPreferencesUtil.getInstance().getString(Config.PAGE_METHOD, Config.PAGE_SLIDE);
    }

    public static int s_lineSpace = -1;
    public static void setLineSpace(int lineSpace) {
        s_lineSpace = lineSpace;
        SharedPreferencesUtil.getInstance().putInt(LINE_SPACE, lineSpace);
    }

    public static int getLineSpace(){
        if (s_lineSpace != -1) {
            return s_lineSpace;
        }
        return SharedPreferencesUtil.getInstance().getInt(LINE_SPACE, 1);
    }
}
